<?php

namespace app\api\controller\xyb;

use app\api\service\UsersService;
use app\common\controller\Api;
use think\helper\Time;
use think\Request;
use app\admin\model\xyb\system\Config;
use app\common\model\circle\User as CircleUser;

/**
 * API控制器基类
 */
class Base extends Api
{
    protected $configObj = '';

    public function __construct(Request $request = null)
    {
        $this->request = is_null($request) ? Request::instance() : $request;

        // 控制器初始化
        $this->_initialize();

        $this->configObj = (new Config())->where('id',1)->find();
    }

    /**
     * User:wangyafang
     * Date:2021/07/29
     * @param $map 搜索条件
     * @param array $except 排除字段
     * @param string $pre_field 表别名
     */
    protected function _search_filter(&$map, $except = [], $pre_field = '')
    {
        $param   = request()->except($except);
        $request = array_filter($param, 'xyb_filter_search_val');

        $search_field_list = array_keys($request);

        foreach ($search_field_list as $search_field) {
            $prefix = substr($search_field, 0, 3);

            $prefix_list = array('li_', 'eq_', 'gt_', 'lt_', 'in_','is_','no_');

            if (in_array($prefix, $prefix_list)) {
                $field = substr($search_field, 3);

                if ($prefix == "li_") {
                    $map[$pre_field.$field] = array('like', '%' . trim($request[$search_field]) . '%');
                }
                if ($prefix == "eq_") {
                    $map[$pre_field.$field] = ['eq',trim($request[$search_field])];//tp5.0写法
                }
                if ($prefix == "gt_") {
                    $map[$pre_field.$field] = array('egt', trim($request[$search_field]));
                }
                if ($prefix == "lt_") {
                    $map[$pre_field.$field] = array('elt', trim($request[$search_field]));
                }
                if ($prefix == "in_") {
                    $map[$pre_field.$field] = array('in', trim($request[$search_field]));
                }
                if ($prefix == "is_") {//判断字段值是否为空
                    $map[$pre_field.$field] = array('neq', '');
                }
                if ($prefix == "no_") {//不等于
                    $map[$pre_field.$field] = array('neq', trim($request[$search_field]));
                }
            }

            $suffix = substr($search_field, -6);
            if ($suffix == '_range') {
                $field = substr($search_field, 0, -6);

                list($start_date, $end_date) = explode("~", trim($request[$search_field]));

                $start_date = substr(trim($start_date), 0, 10);
                $end_date   = substr(trim($end_date), 0, 10);

                $map[$pre_field.$field] = ['between time', [$start_date, date('Y-m-d H:i:s', strtotime($end_date) + 3600 * 24)]];
            }

            if ($search_field == 'time') {
                $prefix_list = array('d', 'w', 'm', 'y');
                $time = Time::today();

                if (in_array($prefix, $prefix_list)) {
                    if ($request[$search_field] == 'd') {
                        $time = Time::today();
                    }
                    if ($request[$search_field] == 'w') {
                        $time = Time::week();
                    }
                    if ($request[$search_field] == 'm') {
                        $time = Time::month();
                    }
                    if ($request[$search_field] == 'y') {
                        $time = Time::year();
                    }
                    list($start, $end) = $time;
                    $map['create_time'] = array('between time', [$start, $end]);
                }
            }
        }
    }

    public function getAuthUsers(){

//     $user_id = request()->header('user-id', 2);
//     return \app\admin\model\User::get($user_id);
        if($user_id = request()->header('user-id', 0)){
            $users = \app\admin\model\User::get($user_id);
        } else {
            $users = $this->auth->getUserinfoObject();
        }
       // $users = $this->auth->getUserinfoObject();
        //$user_circle_id = UsersService::getUsersCircleId($users->id, $users->circle_id);
//        var_dump($users->circle_id);
//        if($users->circle_id){
//            if(!$circle_user = CircleUser::where(['user_id' => $user_id, 'circle_id' => $users->circle_id])->find()){
//                $circle_user = CircleUser::where(['user_id' => $user_id])->order('id desc')->find();
//                if($circle_user){
//                    $users->circle_id = $circle_user->circle_id;
//                } else {
//                    $users->circle_id = 0;
//                }
//            }
//        }
//        var_dump($users->circle_id);
        return $users;
    }
}
